package com.jshongli.myapplication.task;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jshongli.myapplication.R;
import com.jshongli.myapplication.base.BaseActivity;
import com.jshongli.myapplication.common.callback.UpdateCallback;
import com.jshongli.myapplication.common.utils.AccountManager;
import com.jshongli.myapplication.common.utils.StringUtils;
import com.jshongli.myapplication.common.utils.SuccessDialogUtils;
import com.jshongli.myapplication.common.utils.ToastUtils;
import com.jshongli.myapplication.common.utils.UIUtils;
import com.jshongli.myapplication.common.utils.URLUtils;
import com.jshongli.myapplication.model.base.Update;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.Request;

public class TaskReplyActivity extends BaseActivity {

    @BindView(R.id.et_jdlxr)
    EditText et_jdlxr;

    @BindView(R.id.rg_state)
    RadioGroup rg_state;


    String id ;

    private int status = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_task_reply;
    }

    @Override
    protected void initData() {
        super.initData();
        id = getIntent().getStringExtra("id");


    }

    @Override
    protected void initView() {
        super.initView();

        mToolbar.setTitle("任务反馈");
        initToolbarBack(mToolbar);
        initTvRightText(mToolbar,"提交",null);

        rg_state.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rb_handing) {
                    status = 2;
                } else {
                    status = 3;
                }
            }
        });
    }

    @Override
    protected void onTvRightClicked() {
        super.onTvRightClicked();
        if(StringUtils.editTextIsNull(et_jdlxr)){
            ToastUtils.showCenter(mContext,"请输入反馈信息");
            return;
        }

        fk();
    }

    SuccessDialogUtils tipDialog;

    //region HTTP -- 反馈
    private void fk() {
        String url = URLUtils.URLNoToken("manageAPI/bizReply/add");
        Map<String, String> map = new HashMap<String, String>();
        map.put("reply", et_jdlxr.getText().toString());
        map.put("workId", id);
        map.put("status", String.valueOf(status));
        Gson gson = new Gson();
        final String s = gson.toJson(map);

        OkHttpUtils
                .postString()
                .url(url)
                .addHeader("Authorization", AccountManager.getToken())
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(s)
                .build()
                .execute(new UpdateCallback<Update>(mContext) {

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                        UIUtils.showLoadingProgressDialog(TaskReplyActivity.this, "正在提交...");
                    }

                    @Override
                    public void onAfter(int id) {
                        super.onAfter(id);
                        UIUtils.cancelProgressDialog();
                    }

                    @Override
                    public void onSuccess(Update response) {

                        tipDialog = new SuccessDialogUtils(mActivity, R.style.loading_dialog, "提交成功", R.mipmap.ic_right, true);
                        tipDialog.show();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
//                                if (TaskDetailActivity.mActivity != null) {
//                                    TaskDetailActivity.mActivity.finish();
//                                }
//                                if(TaskActivity.xActivity != null){
//                                    TaskActivity.xActivity.getData();
//                                }
                                Intent intent = new Intent();
                                intent.putExtra("status",String.valueOf(status));
                                setResult(8888, intent);

                                finish();
                                tipDialog.dismiss();
                            }
                        }, 500);
                    }

                    @Override
                    public void onUpdating() {

                    }

                    @Override
                    public void onUpdateFailed() {
                        tipDialog = new SuccessDialogUtils(mActivity, R.style.loading_dialog, "提交失败", R.mipmap.ic_error, true);
                        tipDialog.show();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                tipDialog.dismiss();
                            }
                        }, 500);
                    }


                });
    }
    //endregion
}